package com.lp.mgt.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.checkerframework.checker.units.qual.m;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.lp.mgt.commons.CustomResult;
import com.lp.mgt.entities.Department;
import com.lp.mgt.entities.Menu;
import com.lp.mgt.entities.User;
import com.lp.mgt.services.MenuService;
import com.lp.mgt.utils.ShrioUtil;


@RestController
@RequestMapping("/menu")
public class MenuCtr {
	@Autowired
	private MenuService menuService;
	
	@RequestMapping("/listPage")
	public ModelAndView toUserPage(ModelAndView mv) {
		mv.setViewName("menu/menuList.html");
		return mv;
	}
	
	@RequestMapping("list")
	@RequiresPermissions("menu:list")
    public List<Menu> list(Menu menu)
    {
        List<Menu> menuList = menuService.selectMenuList(menu);
        return menuList;
    }

	@GetMapping("/getAllMenusByRoleId")
	public CustomResult getAllMenusByRoleId(String roleId) {
		List<Menu> nodes=menuService.getAllMenus2LoadTreeWithRoleId(roleId);
		return new CustomResult().success(true, nodes);
	}

	@PostMapping("/divideMenus")//分配权限
	public CustomResult saveMenusWithRoleId(String roleId,String datas) {
		List<Menu> nodes=(List<Menu>) JSON.parse(datas);
		Boolean flag=menuService.changeMenusWithRoleId(roleId,nodes);
		return new CustomResult().success(flag);
	}
	
	@PostMapping("/moveUp")
	public CustomResult moveUp(@RequestBody Menu data) {
		//Menu menu=(Menu) JSON.parse(data);
		Map<String,Object> map=menuService.moveUp(data);
		return new CustomResult().success((Boolean) map.get("flag"), map.get("msg"));
	}
	
	@GetMapping("/removeMenu")
	public CustomResult<Menu> removeMenu(String menuId){
		Boolean flag=menuService.removeMenuByMenuId(menuId);
		return new CustomResult().success(flag);
	}
	
	@PostMapping("/saveMenu")
	public CustomResult<Menu> saveMenu(Menu menu){
		Boolean flag=menuService.saveMenu(menu);
		return new CustomResult().success(flag);
	}
	@PostMapping("/editMenu")
	public CustomResult<Menu> editMenu(Menu menu){
		Boolean flag=menuService.editMenu(menu);
		return new CustomResult().success(flag);
	}

}
